﻿using ConfExpert.Forms.Base;

using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class Ayrimlar : XForm
    {
        BindingSource bsAyrim1, bsAyrim2;
        string tip;
        public Ayrimlar(string _tip = "")
        {
            InitializeComponent();
            tip = _tip;

            this.Text = tip == "K" ? "Kumaş" : (tip == "T" ? "Tela" : "Kumaş+Tela") + " Ayrımları";
        }

        private void Ayrimlar_Load(object sender, EventArgs e)
        {
            //cbEdit.Visible = WhoAmI == 4 || WhoAmI == 17 || WhoAmI == 94;
            //cbEdit.CheckedChanged += new EventHandler(cbEdit_CheckedChanged);
            //btnKaydet.Click += new EventHandler(btnKaydet_Click);

            bsAyrim1 = gvAyrim1.Init("GetAyrimlar4", tip);
            bsAyrim2 = gvAyrim1.AddDetailGrid(gvAyrim2, "AyrimId");

            //gvAyrim1.SutunEkle("RezerveQty", typeof(int), "SUM(Child({FK}).RezerveQty)");
            //gvAyrim1.SutunEkle("CikanQty", typeof(int), "SUM(Child({FK}).CikanQty)");
            //gvAyrim1.SutunEkle("KalanQty", typeof(int), "SUM(Child({FK}).KalanQty)");

            //bsAyrim1 = new BindingSource(ds, "xAyrim1");
            //bsAyrim2 = new BindingSource(bsAyrim1, "FK_xAyrim2_xAyrim1");
            //gvAyrim1.SutunEkle("KalanYuzde", typeof(decimal), "100*KalanQty/RezerveQty");
            gvAyrim1.FormatAsNumber(1, "KalanYuzde");

            gvAyrim1.MakeReadOnly();
            gvAyrim1.FormatAsDate("RzvExpDate");


            gvAyrim1.Columns["AyrimId"].ColumnEdit = null;

            gvAyrim2.MakeColumnInvisible("AyrimId", "ProductId");

            GridManager gm = GridManager.GetManager(gvAyrim1);
            gm.GridMenu.AddMenu("Top Detaylı Liste", DetayGoster);
            //gm.GridMenu.AddMenu("Topların Yerini Göster", ToplarinYeriniGoster_Click);    //OrderRef'a değil AyrimId'ye göre olsun!
            if (ytk(87))
                gm.GridMenu.AddMenu("Düzenle/Birleştir", Duzenle);


            gvAyrim2.OptionsBehavior.Editable = false;
            gvAyrim1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gvAyrimlar_FocusedRowChanged);
            //gvAyrim1.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvAyrimlar_RowStyle);

            //this.Text = "Ayrımlar";   // - " + ((DataRowView)bsAyrim1.Current).Row["OrderRef"].ToString();

            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            btnGuncelle.PerformClick();

            splitContainerControl1.AutoSize();
            //bsAyrim3.Filter = "OrderRef=" + intParse(bsAyrim2, "OrderRef").ToString() + " AND ProductId=" + intParse(bsAyrim2, "ProductId").ToString();


            gvAyrim1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gvAyrim1_RowCellStyle);
            gvAyrim2.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gvAyrim1_RowCellStyle);

            //checkEdit1.CheckedChanged += new EventHandler(checkEdit1_CheckedChanged);
            //bsAyrim1.Filter = "KalanQty>0";

            gvAyrim1.MakeReadOnly();

            gvAyrim1.FocusedRowHandle = gvAyrim1.LocateByDisplayText(0, gvAyrim1.Columns["OrderRef"], OrderRef);

            gvAyrim1.FormatAsNumber(1, "KalanQty", "CikanQty", "RezerveQty");
            gvAyrim1.AddSummary("KalanQty", "CikanQty", "RezerveQty");

            gvAyrim2.FormatAsNumber(1, "KalanQty", "CikanQty", "RezerveQty");
            gvAyrim2.AddSummary("KalanQty", "CikanQty", "RezerveQty");

            gvAyrim1.BestFitColumns();
            gvAyrim2.BestFitColumns();

            checkEdit1.CheckedChanged += checkEdit1_CheckedChanged;
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAyrim1.RefreshDataSource(true, "GetAyrimlar4", tip);
        }

        void checkEdit1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bsAyrim2.Filter = checkEdit1.Checked ? "KalanQty>0" : string.Empty;
        }

        void Duzenle(object sender, EventArgs e)
        {
            string str = "0";
            foreach (int rowHandle in gvAyrim1.GetSelectedRows())
                if (rowHandle > -1)
                    str += "," + gvAyrim1.GetRowCellDisplayText(rowHandle, "AyrimId");




            //TODO: DİKKAT: ATIL durumunu kaldırmıyoruz!
            using (AyrimEdit ae = new AyrimEdit(str))
            {
                if (ae.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    btnGuncelle.PerformClick();
            }
        }


        void DetayGoster(object sender, EventArgs e)
        {
            int AyrimId = intParse(bsAyrim1, "AyrimId");

            using (AyrimlarDetay ad = new AyrimlarDetay(AyrimId, tip))
            {
                ad.ShowDialog();
            }
        }

        void gvAyrim1_DoubleClick(object sender, EventArgs e)
        {
            OrderRef = strParse(bsAyrim1, "OrderRef").Trim();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        //void cbEdit_CheckedChanged(object sender, EventArgs e)
        //{
        //    btnKaydet.Visible = cbEdit.Checked;
        //    gvAyrim1.Columns["YeniOrderRef"].VisibleIndex = cbEdit.Checked ? gvAyrim1.Columns["OrderRef"].VisibleIndex + 1 : -1;
        //}


        void gvAyrim1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            switch (e.Column.FieldName)
            {
                case "KalanQty":
                case "OrderRef":
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    break;
                default:
                    break;
            }
        }

        void btnEdit_Click(object sender, EventArgs e)
        {
            //TODO: v_Top ve eski ayrımları yenisine geçirince bunu aç!
            XtraMessageBox.Show("coming soon..");
            return;

            /* AÇILACAK KISIM
            List<int> vr = new List<int>();
            int dataRowCount = gvAyrim1.DataRowCount;
            for (int i = 0; i < dataRowCount; i++)
                vr.Add(intParse(gvAyrim1.GetRowCellValue(i, "AyrimId")));

            if (vr.Count == 0)
            {
                XtraMessageBox.Show("Lütfen ayrım seçiniz..");
                return;
            }
             */

            //AyrimYeni ayrim = new AyrimYeni(string.Join(",", vr.ToArray()));
            //DialogResult dr = ayrim.ShowDialog();
            //if (dr == System.Windows.Forms.DialogResult.OK)
            //    RefreshData();

        }

        private void gvAyrimlar_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (bsAyrim1.Current != null)
            {
                txtInfo.Caption = ((DataRowView)bsAyrim1.Current).Row["OrderRef"].ToString();
                //gvAyrimLines.ExpandAllGroups();
                gvAyrim2.BestFitColumns();
            }
        }


        public string OrderRef;


        void gvAyrimlar_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            //if (gvAyrim1.GetRowCellValue(e.RowHandle, "stCikisId") != null)
            //    if (gvAyrim1.GetRowCellValue(e.RowHandle, "stCikisId").ToString() == "4")
            //    {
            //        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Strikeout);
            //        e.Appearance.ForeColor = Color.Gray; // SystemColors.ControlDark;
            //    }
            //    else if (boolParse(gvAyrim1.GetRowCellValue(e.RowHandle, "toCut")))
            //        e.Appearance.ForeColor = Color.OrangeRed;


        }

        private void ToplarinYeriniGoster_Click(object sender, EventArgs e)
        {
            using (ToplarinYeri toplarinYeri = new ToplarinYeri(gvAyrim1.GetFocusedRowCellValue("OrderRef").ToString()))
                toplarinYeri.ShowDialog();
        }



    }
}